package com.beyond.trace.dy.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.beyond.trace.dy.domain.TraceDyRecord;
import com.beyond.trace.dy.service.ITraceDyRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 产品事件记录Controller
 * 
 * @author Mr.Yao
 * @date 2022-04-08
 */
@RestController
@RequestMapping("/dy/record")
@Api(value = "产品事件记录", tags = "产品事件记录")
public class TraceDyRecordController extends BaseController
{
    @Autowired
    private ITraceDyRecordService traceDyRecordService;

    //@PreAuthorize("@ss.hasPermi('dy:record:list')")
    @GetMapping("/list")
    @ApiOperation("查询产品事件记录列表")
    public TableDataInfo list(TraceDyRecord traceDyRecord)
    {
        startPage();
        List<TraceDyRecord> list = traceDyRecordService.selectTraceDyRecordList(traceDyRecord);
        return getDataTable(list);
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:query')")
    @GetMapping(value = "/query")
    @ApiOperation("查询产品事件记录列表不分页")
    public AjaxResult getInfoQuery(TraceDyRecord traceDyRecord)
    {
        return AjaxResult.success(traceDyRecordService.selectTraceDyRecordList(traceDyRecord));
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:export')")
    @Log(title = "产品事件记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TraceDyRecord traceDyRecord)
    {
        List<TraceDyRecord> list = traceDyRecordService.selectTraceDyRecordList(traceDyRecord);
        ExcelUtil<TraceDyRecord> util = new ExcelUtil<TraceDyRecord>(TraceDyRecord.class);
        return util.exportExcel(list, "产品事件记录数据");
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:query')")
    @GetMapping(value = "/{recordId}")
    @ApiOperation("查询产品事件记录列表")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(traceDyRecordService.selectTraceDyRecordById(recordId));
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:add')")
    @Log(title = "产品事件记录", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增产品事件记录")
    public AjaxResult add(@RequestBody TraceDyRecord traceDyRecord)
    {
        return toAjax(traceDyRecordService.insertTraceDyRecord(traceDyRecord));
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:edit')")
    @Log(title = "产品事件记录", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改产品事件记录")
    public AjaxResult edit(@RequestBody TraceDyRecord traceDyRecord)
    {
        return toAjax(traceDyRecordService.updateTraceDyRecord(traceDyRecord));
    }

    //@PreAuthorize("@ss.hasPermi('dy:record:remove')")
    @Log(title = "产品事件记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    @ApiOperation("删除产品事件记录")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(traceDyRecordService.deleteTraceDyRecordByIds(recordIds));
    }

    @Log(title = "图片上传", businessType = BusinessType.INSERT)
    @PostMapping("/picture/upload")
    @ApiOperation("图片上传接口")
    public AjaxResult avatar(@RequestParam("file") MultipartFile file) throws IOException {
        try {
            if (!file.isEmpty()) {
                String avatar = FileUploadUtils.upload(RuoYiConfig.getProfile()+"/dy/record", file);
                AjaxResult ajax = AjaxResult.success();
                ajax.put("path", avatar);
                return ajax;
            } else {
                return AjaxResult.error("上传图片异常，请联系管理员");
            }
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
